package com.desiy.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Desiy
 * @date 2020/5/19 10:07
 */
@Configuration
public class ShiroConfig{
    @Bean
    public LocaleResolver getLocaleResolver(){
        return new MyLocaleResolver();
    }

    //第一步：创建Realm对象,需要自定义类
    @Bean
    public AdminRealm adminRealm(){
        return new AdminRealm();
    }
    //第二步：DafaultWebSecurityManger
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("adminRealm") AdminRealm adminRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联AdminRealm
        securityManager.setRealm(adminRealm);
        return securityManager;
    }
    //第三步：ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        Map<String, String> filterMap = new LinkedHashMap<>();
        /*
        anon：无需认证就可以访问
        authc：必须认证才能访问
        user：必须拥有 记住我 给你才能使用
        perms：拥有对某个资源的权限才能访问
        role：拥有某个角色权限才能访问
         */

        filterMap.put("/user/*","authc");
        bean.setFilterChainDefinitionMap(filterMap);
        bean.setLoginUrl("/user/login");
        return bean;
    }
}
